using UnityEditor;
using System.IO;
using UnityEngine;
using System.Text;
using UnityEngine.AI;
using System.Collections.Generic;
using ServerTools;

public class ExportNavMeshPathGround
{
    private static string outFilePath = "path_ground.dat";
    private static List<Triangle> listTriangle = new List<Triangle>();
    private static int baseTag = 0;
    private static float dis = 0.05000f;
    private static int startI = 0;
    private static int endI = 113;
    [MenuItem("Zhbd_Tools/Export_NavMeshData_Path_Ground")]
    static void Export_NavMeshData_Path_Ground()
    {
        StringBuilder str = new StringBuilder();
        FileStream outFile = new FileStream(Application.dataPath + "/" + outFilePath, FileMode.Create, FileAccess.ReadWrite);

        listTriangle.Clear();
        ServerPath.getInstance().listDraw.Clear();

        NavMeshTriangulation nmt = UnityEngine.AI.NavMesh.CalculateTriangulation();
        for (int i = 0; i < nmt.vertices.Length; ++i)
        {
            str.Append("x: " + nmt.vertices[i].x + ",y: " + nmt.vertices[i].y + ",z: " + nmt.vertices[i].z + "\n");
        }
        str.Append("\nindices:");
        for (int i = 0; i < nmt.indices.Length; ++i)
        {
            str.Append(nmt.indices[i] + ",");
        }
        str.Append("\nareas:");
        for (int i = 0; i < nmt.areas.Length; ++i)
        {
            str.Append(nmt.areas[i] + ",");
        }
        Debug.Log(str.ToString());
        byte[] bytes = Encoding.UTF8.GetBytes(str.ToString());
        outFile.Write(bytes, 0, bytes.Length);
        outFile.Close();
        for (int i = 0; i < nmt.indices.Length; i += 3)
        {
            Triangle t = new Triangle();
            t.A = nmt.vertices[nmt.indices[i]];
            t.B = nmt.vertices[nmt.indices[i + 1]];
            t.C = nmt.vertices[nmt.indices[i + 2]];
            listTriangle.Add(t);
        }
        ServerPath.getInstance().listDraw.Add(listTriangle[baseTag]);
        listTriangle.RemoveAt(baseTag);
        for (int i = 0; i < ServerPath.getInstance().listDraw.Count; ++i)
        {
            for (int j = 0; j < listTriangle.Count; ++j)
            {
                if ((ServerPath.getInstance().listDraw[i].A - listTriangle[j].A).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].A - listTriangle[j].B).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].A - listTriangle[j].C).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].B - listTriangle[j].A).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].B - listTriangle[j].B).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].B - listTriangle[j].C).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].C - listTriangle[j].A).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].C - listTriangle[j].B).magnitude < dis ||
                   (ServerPath.getInstance().listDraw[i].C - listTriangle[j].C).magnitude < dis)
                {
                    ServerPath.getInstance().listDraw.Add(listTriangle[j]);
                    listTriangle.RemoveAt(j);
                    --j;
                }
            }
        }

        endI = ServerPath.getInstance().listDraw.Count - 1;
        for (int i = 0; i < ServerPath.getInstance().listDraw.Count; ++i)
        {
            Debug.DrawLine(ServerPath.getInstance().listDraw[i].A, ServerPath.getInstance().listDraw[i].B, Color.green, 5);
            Debug.DrawLine(ServerPath.getInstance().listDraw[i].A, ServerPath.getInstance().listDraw[i].C, Color.green, 5);
            Debug.DrawLine(ServerPath.getInstance().listDraw[i].C, ServerPath.getInstance().listDraw[i].B, Color.green, 5);
            
        }
        Debug.DrawLine(ServerPath.getInstance().listDraw[0].A, ServerPath.getInstance().listDraw[0].B, Color.red, 5);
        Debug.DrawLine(ServerPath.getInstance().listDraw[0].A, ServerPath.getInstance().listDraw[0].C, Color.red, 5);
        Debug.DrawLine(ServerPath.getInstance().listDraw[0].C, ServerPath.getInstance().listDraw[0].B, Color.yellow, 5);
        Debug.Log("AB和AC的夹角: " + Vector3.Angle((ServerPath.getInstance().listDraw[0].B - ServerPath.getInstance().listDraw[0].A),
            (ServerPath.getInstance().listDraw[0].C - ServerPath.getInstance().listDraw[0].A)));
        Debug.Log("AC和AB的夹角: " + Vector3.Angle((ServerPath.getInstance().listDraw[0].C - ServerPath.getInstance().listDraw[0].A),
            (ServerPath.getInstance().listDraw[0].B - ServerPath.getInstance().listDraw[0].A)));
        Debug.Log("AB和CA的夹角: " + Vector3.Angle((ServerPath.getInstance().listDraw[0].B - ServerPath.getInstance().listDraw[0].A),
            (ServerPath.getInstance().listDraw[0].A - ServerPath.getInstance().listDraw[0].C)));
        
        countG(ServerPath.getInstance().listDraw[startI]);
        countG(ServerPath.getInstance().listDraw[endI]);
        Debug.Log("listDraw.Count: " + ServerPath.getInstance().listDraw.Count);
        ServerPath.getInstance().initNeighbour();
        ServerPath.getInstance().countPath(0, 51);
    }
    public static void countG(Triangle t)
    {
        float my_a = (t.B - t.C).magnitude;
        float my_b = (t.B - t.C).magnitude;
        float my_c = (t.B - t.C).magnitude;

        Vector3 myCenter = new Vector3((my_a * t.A.x + my_b * t.B.x + my_c * t.C.x) / (my_a + my_b + my_c),
            (my_a * t.A.y + my_b * t.B.y + my_c * t.C.y) / (my_a + my_b + my_c),
            (my_a * t.A.z + my_b * t.B.z + my_c * t.C.z) / (my_a + my_b + my_c));
        Debug.DrawRay(myCenter, Vector3.up * 1, Color.red, 1);
    }
    
}
